using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace MicroMsg.UI.UserContrl
{
	public class BaseChatElement : ContentControl
	{
		private const string TAG = "BaseChatElement";
		protected bool mIsLeft = true;
		private HeadImage headImg;
		private StackPanel ItemPanel;
		private Image FlagImg;
		protected BaseChatContent ChatContentCtrl;
		public string cuImgUser;
		private bool isHeadImgMouseDown;
		protected bool mIsGlittleing;
		public static DependencyProperty ExFlagVisibilityProperty = DependencyProperty.Register("ExFlagVisibility", typeof(Visibility), typeof(BaseChatElement), new PropertyMetadata(0, new PropertyChangedCallback(BaseChatElement.OnExFlagVisibilityPropertyChanged)));
		private ImageSource mHeadSrc;
		protected ChatMsg chatMsg_0;
		protected ChatItemClick mclickEvent;
		public bool IsGlittleing
		{
			get
			{
				return this.mIsGlittleing;
			}
			set
			{
				if (value == this.mIsGlittleing)
				{
					return;
				}
				this.mIsGlittleing = value;
				this.GlittleingChange(value);
			}
		}
		public Visibility ExFlagVisibility
		{
			get
			{
				return (Visibility)base.GetValue(BaseChatElement.ExFlagVisibilityProperty);
			}
			set
			{
				base.SetValue(BaseChatElement.ExFlagVisibilityProperty, value);
			}
		}
		public BaseChatElement()
		{
			base.set_CacheMode(null);
			base.add_Loaded(new RoutedEventHandler(this.BaseChatElement_Loaded));
			base.add_Hold(new EventHandler<GestureEventArgs>(this.BaseChatElement_Hold));
		}
		private void BaseChatElement_Hold(object sender, GestureEventArgs e)
		{
			if (this.chatMsg_0.GetStatus() == MsgUIStatus.Processing)
			{
				e.set_Handled(true);
			}
		}
		private Storyboard getLoadStoryBoard()
		{
			double num = 0.0;
			double num2 = 1.0;
			TimeSpan timeSpan = TimeSpan.FromSeconds(0.8);
			ExponentialEase exponentialEase = new ExponentialEase();
			exponentialEase.set_EasingMode(2);
			IEasingFunction easingFunction = exponentialEase;
			Storyboard storyboard = new Storyboard();
			DoubleAnimation doubleAnimation = new DoubleAnimation();
			doubleAnimation.set_From(new double?(num));
			doubleAnimation.set_To(new double?(num2));
			doubleAnimation.set_Duration(timeSpan);
			doubleAnimation.set_EasingFunction(easingFunction);
			DoubleAnimation doubleAnimation2 = doubleAnimation;
			Storyboard.SetTarget(doubleAnimation2, this);
			Storyboard.SetTargetProperty(doubleAnimation2, new PropertyPath(UIElement.OpacityProperty));
			storyboard.get_Children().Add(doubleAnimation2);
			return storyboard;
		}
		private void BaseChatElement_Loaded(object sender, RoutedEventArgs e)
		{
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.headImg = (base.GetTemplateChild("headImg") as HeadImage);
			this.ItemPanel = (base.GetTemplateChild("ItemPanel") as StackPanel);
			this.FlagImg = (base.GetTemplateChild("FlagImg") as Image);
			this.ChatContentCtrl = (base.GetTemplateChild("ChatContentCtrl") as BaseChatContent);
			if (this.ChatContentCtrl != null)
			{
				this.ChatContentCtrl.SetChatElement(this);
			}
			this.RefreshHead();
			this.RefreshFlag();
			this.HeadImgEventInit();
			this.refreshMsgToContent();
		}
		protected virtual void RefreshFlag()
		{
			if (this.FlagImg == null)
			{
				return;
			}
			if (this.chatMsg_0 == null)
			{
				return;
			}
			if (this.chatMsg_0.GetStatus() == MsgUIStatus.Processing)
			{
				this.Processing();
				if (this.IsLeftItem())
				{
					this.IsGlittleing = true;
					return;
				}
			}
			else
			{
				if (this.chatMsg_0.GetStatus() == MsgUIStatus.Fail)
				{
					Log.smethod_5("BaseChatElement", string.Concat(new object[]
					{
						"msg status is failed:msg.localid:",
						this.chatMsg_0.nMsgLocalID,
						"\nmsg.nStatus:",
						this.chatMsg_0.nStatus,
						"\nmsg.nMsgType:",
						this.chatMsg_0.nMsgType,
						"\nmsg.strTalker:",
						this.chatMsg_0.strTalker,
						"\n"
					}));
					this.SetFlagFail();
					if (this.IsLeftItem())
					{
						this.IsGlittleing = false;
						return;
					}
				}
				else
				{
					this.SetFlagSuccess();
					if (this.IsLeftItem())
					{
						this.IsGlittleing = false;
					}
				}
			}
		}
		protected virtual void SetFlagSuccess()
		{
			if (this.FlagImg == null)
			{
				return;
			}
			this.FlagImg.set_Visibility(1);
			this.ExFlagVisibility = 0;
		}
		private bool IsLeftItem()
		{
			return this is ChatLeftText || this is ChatLeftImg || this is ChatLeftVoice;
		}
		protected virtual void Processing()
		{
			if (this.FlagImg == null)
			{
				return;
			}
			if (!this.IsLeftItem())
			{
				if (this.chatMsg_0.IsImage())
				{
					this.FlagImg.set_Visibility(1);
				}
				else
				{
					this.FlagImg.set_Visibility(0);
					Binding binding = new Binding("CHAT_SENDING_IMG_FLAG");
					binding.set_Source(R.theme);
					this.FlagImg.SetBinding(Image.SourceProperty, binding);
				}
			}
			else
			{
				this.FlagImg.set_Visibility(1);
			}
			this.ExFlagVisibility = 1;
		}
		protected virtual void SetFlagFail()
		{
			if (this.FlagImg == null)
			{
				return;
			}
			this.ExFlagVisibility = 1;
			this.FlagImg.set_Visibility(0);
			this.FlagImg.set_Source(ImageCacheMgr.loadResImage("Resource/Img/Chat/senderror.png"));
		}
		private void HeadImgEventInit()
		{
			if (this.headImg == null)
			{
				return;
			}
			this.headImg.add_Click(new RoutedEventHandler(this.headImg_Click));
			this.headImg.add_Hold(new EventHandler<GestureEventArgs>(this.headImg_Hold));
		}
		private void headImg_Click(object sender, RoutedEventArgs e)
		{
			if (this.cuImgUser == null)
			{
				return;
			}
			if (this.cuImgUser.Contains("@bottle"))
			{
				BotterDetailInfo.GotoThisPage(this.cuImgUser, false);
				return;
			}
			Contact contact = StorageMgr.contact.method_0(this.cuImgUser);
			if (contact == null)
			{
				return;
			}
			this.GoToContactDetailPage(contact);
		}
		public void GoToContactDetailPage(Contact contact)
		{
			if (ToolFunction.checkIsStrSystemPlugin(contact.strUsrName))
			{
				if (contact.strUsrName.CompareTo("fmessage") == 0)
				{
					ToolFunction.GotoPageForUserControl("/Source/UI/Contact/FriendsIntroducePluginView.xaml", null, false);
					return;
				}
			}
			else
			{
				if (contact.strUsrName.EndsWith("@qqim"))
				{
					ToolFunction.GotoPageForUserControl("/Source/Plugin/QQOfflineMsg/UI/QQFriendInfoView.xaml", new object[]
					{
						contact.strUsrName
					}, false);
					return;
				}
				ContactDetailFromType fromType = ContactDetailFromType.FROM_MMCONTACT;
				if (this.chatMsg_0 != null && this.chatMsg_0.strTalker != null && this.chatMsg_0.strTalker.EndsWith("@chatroom"))
				{
					fromType = ContactDetailFromType.FROM_CHATGROUP;
				}
				object[] array = new object[]
				{
					contact
				};
				ContactDetailViewForAll.GotoContactDitailView(null, null, contact, fromType, false);
			}
		}
		private void headImg_Hold(object sender, GestureEventArgs e)
		{
			if (ChatContainerPage.getCuChatContainerPage() != null)
			{
				ChatContainerPage.getCuChatContainerPage().mChatPageCtrl.bIsChatListLayoutHold = false;
				Contact contact = StorageMgr.contact.method_0(this.cuImgUser);
				if (contact != null)
				{
					ChatContainerPage.getCuChatContainerPage().mChatPageCtrl.AtMemberFromItemHeadImg(contact.strNickName);
				}
			}
			e.set_Handled(true);
		}
		protected virtual void GlittleingChange(bool newValue)
		{
		}
		private void RefreshHead()
		{
			if (this.headImg == null)
			{
				return;
			}
			if (this.mHeadSrc == null)
			{
				return;
			}
			this.headImg.HeadImgSource = this.mHeadSrc;
		}
		private static void OnExFlagVisibilityPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == BaseChatElement.ExFlagVisibilityProperty)
			{
				(dependencyObject_0 as BaseChatElement).OnExFlagVisibilityPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void OnExFlagVisibilityPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		public virtual void SetMsgContent(string msgContent)
		{
		}
		public virtual void SetImgSrc(string ImgSrc)
		{
		}
		public void SetHeadSrc(ImageSource HeadSrc)
		{
			this.mHeadSrc = HeadSrc;
			this.RefreshHead();
		}
		public virtual void SetChatMsg(ChatMsg Msg)
		{
			this.chatMsg_0 = Msg;
			this.RefreshFlag();
			this.refreshMsgToContent();
		}
		public virtual void SetProgress(int Progress)
		{
		}
		public virtual void SetVoiceLen(int voiceLen)
		{
		}
		public virtual void SetUnReadFlag(MsgReadStatus status)
		{
		}
		public void SetItemClick(ChatItemClick clickEvent)
		{
			this.mclickEvent = clickEvent;
		}
		protected void refreshMsgToContent()
		{
			if (this.ChatContentCtrl == null)
			{
				return;
			}
			this.ChatContentCtrl.SetChatMsg(this.chatMsg_0);
		}
	}
}
